package com.woniu.controller;

import com.woniu.pojo.*;
import com.woniu.service.DutyService;
import com.woniu.service.UserService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("EduManage")//教务管理
public class DutyController {
    @Resource
    private DutyService dutyService;
    @Resource
    private UserService userService;

    /**
     * 值班列表展示页面(并获取userList  下拉需要user.uname)
     * @return
     */
    @RequestMapping("dutylist")
    public String dutylist(Model model) {
        //user列表
        List<User> userList=new ArrayList<>();
        userList=userService.findAll4workselect();
        model.addAttribute("userList", userList);
        //duty列表的所有数据
        List<Duty> dutyList=new ArrayList<>();
        dutyList=dutyService.finAll4dutyselect();
        model.addAttribute("dutyList", dutyList);
        return "EduManage/dutylist";
    }

    /**
     * 根据dno查一个duty
     * @param dno
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Map<String,Object> dutySelectByDno(Integer dno){
        Map<String,Object> resultMap=new HashMap<>();
        try {
            Duty duty = dutyService.findOne4Duty(dno);
            resultMap.put("duty",duty);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }



    /**
     *  值班列表
     *      分页查询
     * @param pageBean
     * @param duty
     * @return
     */
    @RequestMapping("dutylistDo")
    @ResponseBody
    public Map<String, Object> dutylistDo(PageBean pageBean, Duty duty) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Duty> dutyList=dutyService.getDutyList(pageBean, duty);
            resultMap.put("dutyList", dutyList);
            resultMap.put("pageBean", pageBean);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     *   查询用户的值班信息
     * @param pageBean
     * @param duty
     * @return
     */
    @RequestMapping("dutylistByUidDo")
    @ResponseBody
    public Map<String, Object> dutylistByUidDo(PageBean pageBean, Duty duty,HttpSession session) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<Duty> dutyList=dutyService.getDutyListByUid(pageBean, duty,session);
            resultMap.put("dutyList", dutyList);
            resultMap.put("pageBean", pageBean);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     *  新增值班信息
     * @param duty
     * @return
     */
    @RequestMapping("dutySaveDo")
    @ResponseBody
    public Map<Object, Object> dutySaveDo(Duty duty, HttpSession session){
            Map<Object, Object> resultMap=new HashMap<>();
        try {
            dutyService.dutySave(duty,session);
            resultMap.put("result",true);
        } catch (Exception e) {
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     *
     */
    @RequestMapping("dutyEditDo")
    @ResponseBody
    public Map<String, Object> dutyEditDo(Duty duty){
        Map<String, Object> resultMap=new HashMap<>();
        try {
            dutyService.dutyEdit(duty);
            resultMap.put("result",true);
        } catch (Exception e) {
            resultMap.put("result",false);
        }
        return  resultMap;
    }

    /**
     * 前端传输的date数据
     * @param binder
     * @param request
     */
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }
}
